﻿using System;
using System.Linq;

namespace DemoWebApp
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (suggestor1.ShowLastUsedWords)
                SetLastUsedWords();

            var assembly = System.Reflection.Assembly
                .GetAssembly(typeof(AutoCompleteControl.AutoCompleteControl));

            versionLabel.Text = string.Empty;
            versionLabel.Text =
                string.Format("{0} v {1}.{2}.{3}"
                , assembly.GetName().Name
                , assembly.GetName().Version.Major
                , assembly.GetName().Version.Minor
                , assembly.GetName().Version.Build
                );
        }

        private void SetLastUsedWords()
        {
            if (suggestor1.ShowLastUsedWords)
            {
                suggestor1.LastUsedWordList = StaticDatasource.MyDatasource.Where(d => d.WordId <= 3).ToList();
                dataRepeater.DataSource = suggestor1.LastUsedWordList;
                dataRepeater.DataBind();
            }
            else
            {
                dataRepeater.DataSource = null;
                dataRepeater.DataBind();
            }
        }

        protected void getValuesButton_Click(object sender, EventArgs e)
        {
            GetValues();
        }

        private void GetValues()
        {
            var values = suggestor1.ValuesAsInt;
            var newvalues = suggestor1.AddedValues;

            var result = from w in StaticDatasource.MyDatasource
                         where values.Contains(w.WordId)
                         select w;

            dataRepeater.DataSource = result.ToList();
            dataRepeater.DataBind();

            selectedValsLabel.Text = string.Join(", ", values.Select(x => x.ToString()).ToArray());
            newlyAddedValsLabel.Text = string.Join(", ", newvalues.ToArray());
            timerLabel.Text = DateTime.Now.ToString();
            UpdatePanel1.Update();
        }

        protected void suggestor1_ItemSelected(object sender, EventArgs e)
        {
            GetValues();
        }

        protected void suggestor1_ItemDeleted(object sender, EventArgs e)
        {
            GetValues();
        }
    }
}